package com.handsome.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.handsome.entity.HsDictionary;
import com.handsome.entity.HsWidget;
import com.handsome.service.DictionaryService;
import com.handsome.service.WidgetService;
import com.handsome.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DictionaryController extends BaseController {

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private WidgetService widgetService;

    private ObjectMapper objectMapper = new ObjectMapper();


    @RequestMapping("/dictionary/index")
    public String index(Model model) {

        try {
            model.addAttribute("dictionaryItems", new ObjectMapper().writeValueAsString(dictionaryService.getAllDictionary()));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return "dictionary/index";
    }


    @PostMapping("/dictionary/del")
    @ResponseBody
    public Result delDictionaryItem(Integer id) {

        return dictionaryService.delDictionaryItem(id);
    }

    @PostMapping("/dictionary/add_dictionary_item")
    @ResponseBody
    public Result addDictionaryItem(HsDictionary dictionary) {
        return dictionaryService.addDictionaryItem(dictionary);
    }


}
